/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import com.nokia.maps.map.MapCanvas;
import com.nokia.maps.common.*;
import com.nokia.maps.map.MapStandardMarker;
import com.nokia.maps.map.MapContainer;
import com.nokia.maps.map.MapPolygon;
import com.nokia.maps.map.MapPolyline;
import com.nokia.maps.map.MapObject;
import com.nokia.maps.common.GeoBoundingBox;

/**
 * @author juksilve
 */
public class SimpleContent extends MIDlet 
implements CommandListener
{

    private final Command menuCommand;
    private final Command exitCommand;
    private final Command backCommand;
    private final Command selectCommand;
    private final Command selListCommand;
    
    int cuntter = 0;
    int selllected = 0;
    Display display = null;
    List menu = null; // main menu
    List selList = null; // selection menu
    MapCanvas mapCanvas = null; 
    
    MapContainer MapMarker = null;    
    MapPolygon polygon = null;
    MapPolyline polyline = null;
             
    public SimpleContent()
    {
        menuCommand = new Command("Options", Command.ITEM, 1);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        backCommand = new Command("Back", Command.BACK, 1);
        selectCommand = new Command("Open", Command.ITEM, 1); 
        selListCommand = new Command("Select", Command.ITEM, 1); 
     
    }
    
    public void startApp() {
        // Get your own app_id and token by registering at
	// https://api.developer.nokia.com/ovi-api/ui/registration
//	ApplicationContext.getInstance().setAppID("My-id");
//	ApplicationContext.getInstance().setToken("My-Token");

        menu = new List("Menu Items", Choice.IMPLICIT);
        menu.append("Markers", null);
        menu.append("Polygon", null);
        menu.append("Polyline", null);
        
        menu.setSelectCommand(selectCommand);
        menu.addCommand(backCommand);
        menu.setCommandListener(this);
        
	display = Display.getDisplay(this);
        mapCanvas = new MapCanvas(display) {
            public void onMapUpdateError(String string, Throwable thrwbl, boolean bln) {}
            public void onMapContentComplete() {}
        };
        display.setCurrent(mapCanvas);

        mapCanvas.getMapDisplay().setZoomLevel(11, 0, 0);    
        mapCanvas.getMapDisplay().setCenter(new GeoCoordinate(60.26,24.7, 0));
        
        mapCanvas.setTitle("Simple Map content");
        mapCanvas.addCommand(exitCommand);
        mapCanvas.addCommand(menuCommand);
        mapCanvas.setCommandListener(this);
               
        // add own component to get key & touch events
        mapCanvas.getMapDisplay().addMapComponent(new CustomMapComponent(mapCanvas.getMapFactory()));
        
        System.out.println("We are started");          
    }
    
    
    public void commandAction(Command command, Displayable displayable)
    {
        System.out.println("command action: " + command.getLabel());

        if (command == exitCommand){
            System.out.println("Exit");
            notifyDestroyed();
        }else if(command == backCommand){
            System.out.println("We go back to canvas");
            display.setCurrent(mapCanvas);
        }else if(command == menuCommand){
            System.out.println("We show menu");
            display.setCurrent(menu);
        }else if(command == selectCommand){

            System.out.println("We see what is selected with menu");
            
            selllected = menu.getSelectedIndex();

            selList = new List("Edit", Choice.IMPLICIT);
            selList.setSelectCommand(selListCommand);
            selList.addCommand(backCommand);
            selList.setCommandListener(this);
            
            selList.append("Add",null);
            selList.append("Z-Top",null);
            selList.append("Z-Bottom",null);
            selList.append("Toggle visibility",null);
            selList.append("Zoom to",null);
            selList.append("Zoom all",null);
            
            if(selllected == 0){
               System.out.println("Markers");
               selList.setTicker(new Ticker("Markers"));
               
               selList.append("Remove One",null);
               selList.append("Remove all",null);
               
               display.setCurrent(selList);
            }else if(selllected == 1){
                System.out.println("Polygon");
                selList.setTicker(new Ticker("Polygon"));
  
                selList.append("Remove",null);
                
                display.setCurrent(selList);
            }else if(selllected == 2){
                System.out.println("Polyline");
                selList.setTicker(new Ticker("Polyline"));

                selList.append("Remove",null);
                
                display.setCurrent(selList);
            }else {
                Alert alert = new Alert("", "Uknown Selection", null, AlertType.INFO);    
                display.setCurrent(alert);
            }
                    
        }else if(command == selListCommand){
            
            System.out.println("Open command selection");
            
            int sellected = selList.getSelectedIndex();
            
            if(selllected == 0){ //Markers
                
                if(sellected == 0){ //Add

                    if(MapMarker == null){
                                                    
                        MapMarker = mapCanvas.getMapFactory().createMapContainer();
                        
                        
                        MapMarker.addMapObject(mapCanvas.getMapFactory().createStandardMarker(new GeoCoordinate(60.27, 24.80, 0 ), 10, "One",0));
                        MapStandardMarker markker = mapCanvas.getMapFactory().createStandardMarker(new GeoCoordinate(60.22, 24.70, 0 ), 10, "One",0);
                        
                      //  markker.setColor(0xFF43A51B);
                        
                        MapMarker.addMapObject(markker);
                                
                        MapMarker.addMapObject(mapCanvas.getMapFactory().createStandardMarker(new GeoCoordinate(60.27, 24.70, 0 ), 10, "One",0));
                        
                        mapCanvas.getMapDisplay().addMapObject( MapMarker );
                        System.out.println("markers added");
                    }else{//already added
                    }

                }else if(sellected == 1){ //Z-Top
                    BringtoTop(0);
                }else if(sellected == 2){ //Z-Bottom
                    SentToBack(0);
                }else if(sellected == 3){ //Toggle visibility
                    if(MapMarker != null){
                        boolean vissible = !MapMarker.isVisible();
                        System.out.println("set MapMarker visible: " + vissible);
                        MapMarker.setVisible(vissible);
                    }
                }else if(sellected == 4){ //Zoom to
                    if(MapMarker != null){
                        System.out.println("get MapMarker box");
                        GeoBoundingBox box0 = MapMarker.getBoundingBox();
                        if(box0 != null){        
                            mapCanvas.getMapDisplay().zoomTo(box0, false); 
                        }else{
                            System.out.println("MapMarker box is NULL");
                        }
                    }
                }else if(sellected == 5){ //Zoom all
                    mapZoomToAllContent();
                }else if(sellected == 6){ //Remove One
                    if(MapMarker != null){
                        MapObject[] markkers = MapMarker.getAllMapObjects();
                        
                        if(markkers.length > 0){
                            MapMarker.removeMapObject(markkers[0]); 
                            System.out.println("removed one marker");
                        }else{
                            mapCanvas.getMapDisplay().removeMapObject(MapMarker);
                            MapMarker = null;
                        }
                    }
                }else if(sellected == 7){ //Remove all
                    if(MapMarker != null){
                        mapCanvas.getMapDisplay().removeMapObject(MapMarker);
                        MapMarker = null;
                        System.out.println("removed all markers");
                    }
                }else {
                    Alert alert = new Alert("", "Uknown command", null, AlertType.INFO);    
                    display.setCurrent(alert);
                }  
                
                
                        
            }else if(selllected == 1){//Polygon
                if(sellected == 0){ //Add
                    if(polygon == null){
                        GeoCoordinate[] polygonCoord = new GeoCoordinate[3];
                        polygonCoord[0] = new GeoCoordinate(60.22, 24.81, 0);
                        polygonCoord[1] = new GeoCoordinate(60.30, 24.70, 0);
                        polygonCoord[2] = new GeoCoordinate(60.14, 24.57, 0);
                       
                        
                        
                        polygon = mapCanvas.getMapFactory().createMapPolygon(polygonCoord);
                        polygon.setColor(0xAA43A51B);
                        
                        mapCanvas.getMapDisplay().addMapObject(polygon);
                        System.out.println("polygon added");
                    }else{//already added   
                    }      
                }else if(sellected == 1){ //Z-Top
                    BringtoTop(1);
                }else if(sellected == 2){ //Z-Bottom
                    SentToBack(1);
                }else if(sellected == 3){ //Toggle visibility
                    if(polygon != null){
                        boolean vissible = !polygon.isVisible();
                        System.out.println("set polygon visible: " + vissible);
                        polygon.setVisible(vissible);
                    }
                }else if(sellected == 4){ //Zoom to
                    if(polygon != null){
                        System.out.println("get polygon box");
                        GeoBoundingBox box1 = polygon.getBoundingBox();
                        if(box1 != null){        
                            mapCanvas.getMapDisplay().zoomTo(box1, false); 
                        }else{
                            System.out.println("polygon box is NULL");
                        }
                    }
                }else if(sellected == 5){ //Zoom all
                    mapZoomToAllContent();
                }else if(sellected == 6){ //Remove 
                    if(polygon != null){
                        mapCanvas.getMapDisplay().removeMapObject(polygon);
                        polygon = null;
                        System.out.println("removed polygon");
                    }
                }else {
                    Alert alert = new Alert("", "Uknown command", null, AlertType.INFO);    
                    display.setCurrent(alert);
                }
            }else if(selllected == 2){//Polyline
                if(sellected == 0){ //Add
                    if(polyline == null){
                        GeoCoordinate[] polylinCoord = new GeoCoordinate[4];
                        polylinCoord[0] = new GeoCoordinate(60.27, 24.81, 0);
                        polylinCoord[1] = new GeoCoordinate(60.35, 24.70, 0);
                        polylinCoord[2] = new GeoCoordinate(60.19, 24.57, 0);
                        polylinCoord[3] = new GeoCoordinate(60.27, 24.81, 0);
                        
                        polyline = mapCanvas.getMapFactory().createMapPolyline(polylinCoord);
                        polyline.setColor(0xFF43A5FF);
                        
                        mapCanvas.getMapDisplay().addMapObject(polyline);
                        System.out.println("polyline added");
                    }else{//already added   
                    }
                }else if(sellected == 1){ //Z-Top
                    BringtoTop(2);
                }else if(sellected == 2){ //Z-Bottom
                    SentToBack(2);
                }else if(sellected == 3){ //Toggle visibility
                    if(polyline != null){
                        boolean vissible = !polyline.isVisible();
                        System.out.println("set polyline visible: " + vissible);
                        polyline.setVisible(vissible);
                    }
                }else if(sellected == 4){ //Zoom to
                    if(polyline != null){
                        System.out.println("get polyline box");
                        GeoBoundingBox box2 = polyline.getBoundingBox();
                        if(box2 != null){        
                            mapCanvas.getMapDisplay().zoomTo(box2, false); 
                        }else{
                            System.out.println("polyline box is NULL");
                        } 
                    }
                }else if(sellected == 5){ //Zoom all
                    mapZoomToAllContent();
                }else if(sellected == 6){ //Remove
                    if(polyline != null){
                        mapCanvas.getMapDisplay().removeMapObject(polyline);
                        polyline = null;
                        System.out.println("removed polyline");
                    }
                }else {
                    Alert alert = new Alert("", "Uknown command", null, AlertType.INFO);    
                    display.setCurrent(alert);
                }
            }else{
                Alert alert = new Alert("", "Uknown Selection", null, AlertType.INFO);    
                display.setCurrent(alert);
            }
            
            display.setCurrent(mapCanvas);
        }else{
            Alert alert = new Alert("", "Uknown command", null, AlertType.INFO);    
            display.setCurrent(alert);
        }
    }
    
    public void mapZoomToAllContent() {
     
        System.out.println("mapZoomToAllContent");
        
        if(polyline != null || MapMarker != null || polygon != null){
            
            System.out.println("ok");
            
            GeoBoundingBox bboxx = null; 
                    
            
            System.out.println("new box");
            
            if(polygon != null){
                System.out.println("polygon getBoundingBox");
                
                if(bboxx == null){
                    bboxx = new GeoBoundingBox(polygon.getBoundingBox());
                }else{
                    bboxx = bboxx.merge(polygon.getBoundingBox());
                }
            }
            
            if(polyline != null){
                 System.out.println("polyline getBoundingBox");
                 if(bboxx == null){
                    bboxx = new GeoBoundingBox(polyline.getBoundingBox());
                }else{
                    bboxx = bboxx.merge(polyline.getBoundingBox());
                }
            }

            if(MapMarker != null){
                System.out.println("marker getBoundingBox");
                if(bboxx == null){
                    bboxx = new GeoBoundingBox(MapMarker.getBoundingBox());
                }else{
                    bboxx = bboxx.merge(MapMarker.getBoundingBox());
                }     
            }
              
            if(bboxx != null){
                System.out.println("set it");
                mapCanvas.getMapDisplay().zoomTo(bboxx,false);
            }
        }
        System.out.println("mapZoomToAllContent -- done");
        
    }
    
    public void SentToBack(int which) {

        int markerZ = 0;
        int polyZ = 0;
        int lineZ = 0;

        if (MapMarker != null){
            markerZ = MapMarker.getzIndex();
        }

        if (polyline != null){
            lineZ = polyline.getzIndex();
        }

        if (polygon != null){
            polyZ = polygon.getzIndex();
        }

        if (which == 0 )//"Markers"
        {
            if (markerZ >= lineZ)
                markerZ = lineZ - 1;
            if (markerZ >= polyZ)
                markerZ = polyZ - 1;
        }else if(which == 1)//"Polygon"
        {
            if (polyZ >= markerZ)
                polyZ = markerZ - 1;
            if (polyZ >= lineZ)
                polyZ = lineZ - 1;
        }else if (which == 2)//"Polyline"
        {
            if (lineZ >= markerZ)
                lineZ = markerZ - 1;
            if (lineZ >= polyZ)
                lineZ = polyZ - 1;
        }
        
        if (MapMarker != null){
            MapMarker.setzIndex(markerZ);
            
            MapObject[] markkers = MapMarker.getAllMapObjects();
            for(int i=0; i < markkers.length; i++){
                markkers[i].setzIndex(markerZ);
            }
        }

        if (polyline != null){
            polyline.setzIndex(lineZ);
        }

        if (polygon != null){
            polygon.setzIndex(polyZ);
        }

        System.out.println("Z-order, marker: " + markerZ + ", PolyLine: " + lineZ+ ", Polygon: " + polyZ);
    }

    public void BringtoTop(int which) {

        int markerZ = 0;
        int polyZ = 0;
        int lineZ = 0;

        if (MapMarker != null){
            markerZ = MapMarker.getzIndex();
        }

        if (polyline != null){
            lineZ = polyline.getzIndex();
        }

        if (polygon != null){
            polyZ = polygon.getzIndex();
        }

        if (which == 0 )//"Markers"
        {
            if (markerZ <= lineZ)
                markerZ = lineZ + 1;
            if (markerZ <= polyZ)
                markerZ = polyZ + 1;
        }else if(which == 1)//"Polygon"
        {
            if (polyZ <= markerZ)
                polyZ = markerZ + 1;
            if (polyZ <= lineZ)
                polyZ = lineZ + 1;
        }else if (which == 2)//"Polyline"
        {
            if (lineZ <= markerZ)
                lineZ = markerZ + 1;
            if (lineZ <= polyZ)
                lineZ = polyZ + 1;
        }

        if (MapMarker != null){
            MapMarker.setzIndex(markerZ);
            
            MapObject[] markkers = MapMarker.getAllMapObjects();
            for(int i=0; i < markkers.length; i++){
                markkers[i].setzIndex(markerZ);
            }
        }

        if (polyline != null){
            polyline.setzIndex(lineZ);
        }

        if (polygon != null){
            polygon.setzIndex(polyZ);
        }

    System.out.println("Z-order, marker: " + markerZ + ", PolyLine: " + lineZ+ ", Polygon: " + polyZ);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
}
